﻿using MediatR;
using Tb.App.API.Applications.Command.Cultivation;
using Tb.App.Domain.Rabbit.Cultivation;
using Tb.App.ErrorCode;
using Tb.App.Infrastructure.Interfaces;

namespace Tb.App.API.Applications.CommandHandler.Cultivation
{
    /// <summary>
    /// 删除养殖免疫信息-命令
    /// </summary>
    public class DelimmunityCommandHandler : IRequestHandler<DelimmunityCommand, ApiResult<int>>
    {
        private readonly IBaseRepository<ImmunityModel> immunityRep;

        public DelimmunityCommandHandler(IBaseRepository<ImmunityModel> immunityRep)
        {
            this.immunityRep = immunityRep;
        }
        public async Task<ApiResult<int>> Handle(DelimmunityCommand request, CancellationToken cancellationToken)
        {
            ApiResult<int> result = new ApiResult<int>();

            var info=await immunityRep.GetById(request.Id);
            if (info == null) 
            {
                result.Code=ApiEnum.删除失败;
                result.Message = "要删除的数据不存在";
            }
            else
            {
                info.IsDel = true;
                var a =await immunityRep.UpdateAsync(info);
                if(a>0)
                {
                    result.Code = ApiEnum.删除成功;
                    result.Message = ApiEnum.删除成功.ToString();
                    result.Data=a;
                }
                else
                {
                    result.Code = ApiEnum.删除失败;
                    result.Message = ApiEnum.删除失败.ToString();
                    result.Data = a;
                }

            }
            return result;
        }
    }
}
